using System;
using System.Collections;
using System.Data;
using OutSystems.HubEdition.RuntimePlatform;
using GotDotNet.ApplicationBlocks;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using Contracts;
using System.Collections.Generic;

namespace OutSystems.NssRemotingRadarsAdapter {

    public class CssRemotingRadarsAdapter : IssRemotingRadarsAdapter {

        /// <summary>
        /// Acção que irá obter os eventos registados em radares do tipo &apos;RemotingRadars&apos;
        /// </summary>
        /// <param name="ssUri">URI do Serviço de Radar</param>
        /// <param name="ssEventos">Lista de Eventos</param>
        public void MssObterEventos(string ssUri, out RLEvento_RRRecordList ssEventos) {

            ssEventos = new RLEvento_RRRecordList(null);
            
            TcpChannel ch = new TcpChannel();
            ChannelServices.RegisterChannel(ch, false);
            IRadar radar = (IRadar)Activator.GetObject(typeof(IRadar), ssUri);
            List<IEvent> evts = radar.ReadEvents();
            ILocal local = radar.GetLocalInfo();
            
            foreach (IEvent evt in evts) {
                STEvento_RRStructure newEvt = new STEvento_RRStructure();
                // Event related information
                newEvt.ssId = evt.GetId();
                newEvt.ssVelocidade = (decimal)evt.GetSpeed();
                newEvt.ssImagem = evt.GetPicture();
                newEvt.ssMatricula = evt.GetPlate();

                // Local related information
                newEvt.ssNomeLocal = local.GetName();
                newEvt.ssUIDLocal = local.GetUID();
                newEvt.ssLimiteVelocidade = (decimal)local.GetSpeedLimit();
                newEvt.ssActivo = local.IsActive();
            }

        } // MssObterEventos


    } // CssRemotingRadarsAdapter

} // OutSystems.NssRemotingRadarsAdapter

